<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Control Frames</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Beast">
<link rel="up" href="../using_websocket.html" title="WebSocket">
<link rel="prev" href="messages.html" title="Messages">
<link rel="next" href="timeouts.html" title="Timeouts">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="messages.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_websocket.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="timeouts.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="beast.using_websocket.control_frames"></a><a class="link" href="control_frames.html" title="Control Frames">Control Frames</a>
</h3></div></div></div>
<p>
        Control frames are small (less than 128 bytes) messages entirely contained
        in an individual WebSocket frame. They may be sent at any time by either
        peer on an established connection, and can appear in between continuation
        frames for a message. There are three types of control frames: ping, pong,
        and close.
      </p>
<p>
        A sent ping indicates a request that the sender wants to receive a pong.
        A pong is a response to a ping. Pongs may be sent unsolicited, at any time.
        One use for an unsolicited pong is to inform the remote peer that the session
        is still active after a long period of inactivity. A close frame indicates
        that the remote peer wishes to close the WebSocket connection. The connection
        is considered gracefully closed when each side has sent and received a close
        frame.
      </p>
<p>
        During read operations, Beast automatically reads and processes control frames.
        If a control callback is registered, the callback is notified of the incoming
        control frame. The implementation will respond to pings automatically. The
        receipt of a close frame initiates the WebSocket close procedure, eventually
        resulting in the error code <a class="link" href="../ref/boost__beast__websocket__error.html" title="websocket::error"><code class="computeroutput"><span class="identifier">error</span><span class="special">::</span><span class="identifier">closed</span></code></a> being delivered to the caller
        in a subsequent read operation, assuming no other error takes place.
      </p>
<p>
        A consequence of this automatic behavior is that caller-initiated read operations
        can cause socket writes. However, these writes will not compete with caller-initiated
        write operations. For the purposes of correctness with respect to the stream
        invariants, caller-initiated read operations still only count as a read.
        This means that callers can have a simultaneously active read, write, and
        ping/pong operation in progress, while the implementation also automatically
        handles control frames.
      </p>
<h5>
<a name="beast.using_websocket.control_frames.h0"></a>
        <span class="phrase"><a name="beast.using_websocket.control_frames.control_callback"></a></span><a class="link" href="control_frames.html#beast.using_websocket.control_frames.control_callback">Control Callback</a>
      </h5>
<p>
        Ping, pong, and close messages are control frames which may be sent at any
        time by either peer on an established WebSocket connection. They are sent
        using the functions <a class="link" href="../ref/boost__beast__websocket__stream/ping.html" title="websocket::stream::ping"><code class="computeroutput"><span class="identifier">ping</span></code></a>, <a class="link" href="../ref/boost__beast__websocket__stream/pong.html" title="websocket::stream::pong"><code class="computeroutput"><span class="identifier">pong</span></code></a>. and <a class="link" href="../ref/boost__beast__websocket__stream/close.html" title="websocket::stream::close"><code class="computeroutput"><span class="identifier">close</span></code></a>. To be notified of control
        frames, callers may register a <span class="emphasis"><em>control callback</em></span> using
        <a class="link" href="../ref/boost__beast__websocket__stream/control_callback.html" title="websocket::stream::control_callback"><code class="computeroutput"><span class="identifier">control_callback</span></code></a>. The object provided
        with this option should be callable with the following signature:
      </p>
<pre class="programlisting"><span class="identifier">ws</span><span class="special">.</span><span class="identifier">control_callback</span><span class="special">(</span>
    <span class="special">[](</span><span class="identifier">frame_type</span> <span class="identifier">kind</span><span class="special">,</span> <span class="identifier">string_view</span> <span class="identifier">payload</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// Do something with the payload</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ignore_unused</span><span class="special">(</span><span class="identifier">kind</span><span class="special">,</span> <span class="identifier">payload</span><span class="special">);</span>
    <span class="special">});</span>
</pre>
<p>
        When a control callback is registered, it will be invoked for all pings,
        pongs, and close frames received through either synchronous read functions
        or asynchronous read functions. The type of frame and payload text are passed
        as parameters to the control callback. If the frame is a close frame, the
        close reason may be obtained by calling <a class="link" href="../ref/boost__beast__websocket__stream/reason.html" title="websocket::stream::reason"><code class="computeroutput"><span class="identifier">reason</span></code></a>.
      </p>
<p>
        Unlike regular completion handlers used in calls to asynchronous initiation
        functions, the control callback only needs to be set once. The callback is
        not reset after being called. The same callback is used for both synchronous
        and asynchronous reads. The callback is passive; in order to be called, a
        stream read operation must be active.
      </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
          When an asynchronous read function receives a control frame, the control
          callback is invoked in the same manner as that used to invoke the final
          completion handler of the corresponding read function.
        </p></td></tr>
</table></div>
<h5>
<a name="beast.using_websocket.control_frames.h1"></a>
        <span class="phrase"><a name="beast.using_websocket.control_frames.close_frames"></a></span><a class="link" href="control_frames.html#beast.using_websocket.control_frames.close_frames">Close
        Frames</a>
      </h5>
<p>
        The WebSocket protocol defines a procedure and control message for initiating
        a close of the session. In this procedure, a host requests the close by sending
        a <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1" target="_top"><span class="emphasis"><em>close
        frame</em></span></a>. To request a close use a close function such as
        <a class="link" href="../ref/boost__beast__websocket__stream/close/overload2.html" title="websocket::stream::close (2 of 2 overloads)"><code class="computeroutput"><span class="identifier">close</span></code></a> or <a class="link" href="../ref/boost__beast__websocket__stream/async_close.html" title="websocket::stream::async_close"><code class="computeroutput"><span class="identifier">async_close</span></code></a>:
      </p>
<pre class="programlisting"><span class="identifier">ws</span><span class="special">.</span><span class="identifier">close</span><span class="special">(</span><span class="identifier">close_code</span><span class="special">::</span><span class="identifier">normal</span><span class="special">);</span>
</pre>
<p>
        The close function will send a close frame, read and discard incoming message
        data until receiving a close frame, and then shut down the underlying connection
        before returning.
      </p>
<p>
        When a close frame is received by during a read operation, the implementation
        will automatically respond with a close frame and then shut down the underlying
        connection before returning. In this case, the read operation will complete
        with the code <a class="link" href="../ref/boost__beast__websocket__error.html" title="websocket::error"><code class="computeroutput"><span class="identifier">error</span><span class="special">::</span><span class="identifier">closed</span></code></a>. This indicates to the caller
        that the connection has been closed cleanly.
      </p>
<div class="important"><table border="0" summary="Important">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../../doc/src/images/important.png"></td>
<th align="left">Important</th>
</tr>
<tr><td align="left" valign="top"><p>
          To receive the <a class="link" href="../ref/boost__beast__websocket__error.html" title="websocket::error"><code class="computeroutput"><span class="identifier">error</span><span class="special">::</span><span class="identifier">closed</span></code></a> error, a read operation
          is required.
        </p></td></tr>
</table></div>
<h5>
<a name="beast.using_websocket.control_frames.h2"></a>
        <span class="phrase"><a name="beast.using_websocket.control_frames.auto_fragment"></a></span><a class="link" href="control_frames.html#beast.using_websocket.control_frames.auto_fragment">Auto-fragment</a>
      </h5>
<p>
        To ensure timely delivery of control frames, large outgoing messages can
        be broken up into smaller sized frames. The automatic fragment option turns
        on this feature, and the write buffer size option determines the maximum
        size of the fragments:
      </p>
<pre class="programlisting"><span class="identifier">ws</span><span class="special">.</span><span class="identifier">auto_fragment</span><span class="special">(</span><span class="keyword">true</span><span class="special">);</span>
<span class="identifier">ws</span><span class="special">.</span><span class="identifier">write_buffer_bytes</span><span class="special">(</span><span class="number">16384</span><span class="special">);</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie
      Falco<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="messages.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_websocket.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="timeouts.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
